package com.camerareader.upload_list;

import com.fileoperation.model.Photo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016-12-22.
 */

public class QueueLinked {
    QueueNode front; // 队首指针
    QueueNode rear;  // 队尾指针
    private int count;

    public QueueLinked() {
        this.rear = null;
        this.front = null;
    }   //将一个对象追加到队列的尾部
    public void enqueue(Photo obj) {
        count ++;
        //如果队列是空的
        if (rear == null && front == null) {
            rear = new QueueNode(obj);
            front = rear;
        } else {
            QueueNode node = new QueueNode(obj);
            rear.next = node;
            rear = rear.next;
        }

    }
    //队首对象出队
    //return 出队的对象，队列空时返回null
    public Photo dequeue() {
        //如果队列空
        if (front == null) {
            return null;
        }
        count--;
        //如果队列中只剩下一个对象
        if (front == rear && rear != null) {
            QueueNode node = front;
            rear = null;
            front = null;
            return node.data;
        }
        Photo obj = front.data;
        front = front.next;

        return obj;
    }

    public int getCount() {
        return count;
    }

    public ArrayList<Photo> getQueueList(){
        if (front == null) return null;
        ArrayList<Photo> list = new ArrayList<>(count + 1);
        QueueNode node = front;
        while (true){
            if (node == null) break;
            list.add(node.data);
            node = node.next;
        }
        return list;
    }

    public boolean hasPhoto(Photo photo){
        List<Photo> list = getQueueList();
        if(list != null && list.size() > 0){
            return list.contains(photo);
        }
        return false;
    }
}
